<?php
$this->load->model("core/file");
class ModelAddonBds extends ModelCoreFile
{ 
	public function getItem($bdsid, $where="")
	{
		$bdsid=$this->db->escape(@$bdsid);
		$query = $this->db->query("Select `dlbds`.* 
									from `dlbds` 
									where bdsid ='".$bdsid."' ".$where);
		return $query->row;
	}
	
	public function getList($where="", $from=0, $to=0)
	{
		
		$sql = "Select `dlbds`.* 
									from `dlbds` 
									where status <> 'delete' " . $where;
		if($to > 0)
		{
			$sql .= " Limit ".$from.",".$to;
		}
		
		$query = $this->db->query($sql);
		return $query->rows;
	}
	
	public function getListDel($where="", $from=0, $to=0)
	{
		
		$sql = "Select `dlbds`.* 
									from `dlbds` 
									where status = 'delete' " . $where;
		if($to > 0)
		{
			$sql .= " Limit ".$from.",".$to;
		}
		
		$query = $this->db->query($sql);
		return $query->rows;
	}
	
	private function nextID($prefix)
	{
		return $this->db->getNextIdVarChar("dlbds","bdsid",$prefix);	
	}
	
	public function insert($data)
	{
		//$bdsid = $this->db->escape(@$data['bdsid']);
		$bdsid = $this->nextID("BDS");
		$tieude= $this->db->escape(@$data['tieude']);
		$refersitemap=$this->db->escape(@$data['refersitemap']);
		$nhucau=$this->db->escape(@$data['nhucau']);
		$loaibds=$this->db->escape(@$data['loaibds']);
		$gia=$this->db->escape(@$this->string->toNumber($data['gia']));
		$huong=$this->db->escape(@$data['huong']);
		$khuvuc=$this->db->escape(@$data['khuvuc']);
		$diachi=$this->db->escape(@$data['diachi']);
		$dientich=$this->db->escape(@$this->string->toNumber($data['dientich']));
		$hotenchusohuu=$this->db->escape(@$data['hotenchusohuu']);
		$dienthoai=$this->db->escape(@$data['dienthoai']);
		$email=$this->db->escape(@$data['email']);
		$mota=$this->db->escape(@$data['mota']);
		$imageid=(int)@$data['imageid'];
		$imagepath=$this->db->escape(@$data['imagepath']);
		$attachment=$this->db->escape(@$data['attachment']);
		$status="";
		$statusdate=$this->date->getToday();
		$createdate=$this->date->getToday();
		$createdby=$this->user->getId();
		$assignto=$this->db->escape(@$data['assignto']);
		
		$field=array(
						'bdsid',
						'tieude',
						'refersitemap',
						'nhucau',
						'loaibds',
						'gia',
						'huong',
						'khuvuc',
						'diachi',
						'dientich',
						'hotenchusohuu',
						'dienthoai',
						'email',
						'mota',
						'imageid',
						'imagepath',
						'attachment',
						'status',
						'statusdate',
						'createdate',
						'createdby',
						'assignto'
						
					);
		$value=array(
						$bdsid,
						$tieude,
						$refersitemap,
						$nhucau,
						$loaibds,
						$gia,
						$huong,
						$khuvuc,
						$diachi,
						$dientich,
						$hotenchusohuu,
						$dienthoai,
						$email,
						$mota,
						$imageid,
						$imagepath,
						$attachment,
						$status,
						$statusdate,
						$createdate,
						$createdby,
						$assignto
					);
		$this->db->insertData("dlbds",$field,$value);
		$this->updateFileTemp($imageid);
		//Write log
		$arr = array_combine($field,$value);
		
		$data['logtime'] = $this->date->getToday();
		$data['userid'] = $this->user->getId();
		$data['event'] = "Thêm bất động sản ".$bdsid." - ".$tieude ;
		$data['detail'] = json_encode($arr);
		$this->document->writelog($data);
		
		return $bdsid;
	}
	
	public function update($data)
	{
		$bdsid= $this->db->escape(@$data['bdsid']);
		$tieude= $this->db->escape(@$data['tieude']);
		$refersitemap=$this->db->escape(@$data['refersitemap']);
		$nhucau=$this->db->escape(@$data['nhucau']);
		$loaibds=$this->db->escape(@$data['loaibds']);
		$gia=$this->db->escape(@$this->string->toNumber($data['gia']));
		$huong=$this->db->escape(@$data['huong']);
		$khuvuc=$this->db->escape(@$data['khuvuc']);
		$diachi=$this->db->escape(@$data['diachi']);
		$dientich=$this->db->escape(@$this->string->toNumber($data['dientich']));
		$hotenchusohuu=$this->db->escape(@$data['hotenchusohuu']);
		$dienthoai=$this->db->escape(@$data['dienthoai']);
		$email=$this->db->escape(@$data['email']);
		$mota=$this->db->escape(@$data['mota']);
		$imageid=(int)@$data['imageid'];
		$imagepath=$this->db->escape(@$data['imagepath']);
		$attachment=$this->db->escape(@$data['attachment']);
		$assignto=$this->db->escape(@$data['assignto']);

		$field=array(
						'bdsid',
						'tieude',
						'refersitemap',
						'nhucau',
						'loaibds',
						'gia',
						'huong',
						'khuvuc',
						'diachi',
						'dientich',
						'hotenchusohuu',
						'dienthoai',
						'email',
						'mota',
						'imageid',
						'imagepath',
						'attachment',
						'assignto'
						
					);
		$value=array(
						$bdsid,
						$tieude,
						$refersitemap,
						$nhucau,
						$loaibds,
						$gia,
						$huong,
						$khuvuc,
						$diachi,
						$dientich,
						$hotenchusohuu,
						$dienthoai,
						$email,
						$mota,
						$imageid,
						$imagepath,
						$attachment,
						$assignto
					);
		
		$where="bdsid = '".$bdsid."'";
		$this->db->updateData('dlbds',$field,$value,$where);
		$this->updateFileTemp($imageid);
		
		//Write log
		$arr = array_combine($field,$value);
		$data['logtime'] = $this->date->getToday();
		$data['userid'] = $this->user->getId();
		$data['event'] = "Cập nhật bất động sản ".$bdsid." - ".$tieude ;
		$data['detail'] = json_encode($arr);
		$this->document->writelog($data);
		
		return true;
	}
	
	public function updateStatus($data)
	{
		$bdsid = $this->db->escape(@$data['bdsid']);
		$status=$this->db->escape(@$data['status']);
		$statusdate=$this->date->getToday();
		$field=array(
						
						'status',
						'statusdate'
						
					);
		$value=array(
						
						$status,
						$statusdate
					);
		
		$where="bdsid = '".$bdsid."'";
		$this->db->updateData('dlbds',$field,$value,$where);
		return true;
	}
	
	public function delete($bdsid)
	{
		$data['bdsid'] = $bdsid;
		$data['status'] = 'delete';
		$this->updateStatus($data);
		//Write log
		$data['logtime'] = $this->date->getToday();
		$data['userid'] = $this->user->getId();
		$data['event'] = "Xóa bất động sản ".$bdsid ;
		$data['detail'] = json_encode($arr);
		$this->document->writelog($data);
	}
}
?>